package com.rain.rainspringboot3.desensitize.core.util;

import com.rain.rainspringboot3.desensitize.core.base.annotation.DesensitizeBy;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

public class DesensitizeAnnotationHelper {
    /**
     * 判断字段是否包含脱敏注解
     *
     * @param field 字段
     * @return 是否包含脱敏注解
     */
    public static boolean hasDesensitizeAnnotation(Field field) {
        if (field == null) {
            return false;
        }

        // 遍历字段上的所有注解
        for (Annotation annotation : field.getAnnotations()) {
            // 检查注解是否被DesensitizeBy注解标记
            if (annotation.annotationType().isAnnotationPresent(DesensitizeBy.class)) {
                return true;
            }
        }

        return false;
    }

    /**
     * 获取字段上的脱敏注解
     *
     * @param field 字段
     * @return 脱敏注解，如果不存在则返回null
     */
    public static Annotation getDesensitizeAnnotation(Field field) {
        if (field == null) {
            return null;
        }

        // 遍历字段上的所有注解
        for (Annotation annotation : field.getAnnotations()) {
            // 检查注解是否被DesensitizeBy注解标记
            if (annotation.annotationType().isAnnotationPresent(DesensitizeBy.class)) {
                return annotation;
            }
        }

        return null;
    }
}
